
#!/bin/bash


CURRENT_DIRECTORY=`pwd`
export LIB_ROOT_DIR=${CURRENT_DIRECTORY}/libs
export INSTALL_DIR=${LIB_ROOT_DIR}/ios
export SOURCE_DIR=${LIB_ROOT_DIR}/sources_ios
export BUILD_DIR=${LIB_ROOT_DIR}/build_ios


#export TOOLCHAIN=$NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64

export CC=`xcrun --find clang`
export CXX=${CC}
export AR=`xcrun --find ar`
export AS=`xcrun --find as`
export LD=`xcrun --find ld`
export RANLIB=`xcrun --find RANLIB`
export STRIP=`xcrun --find STRIP`
export LIBTOOL=`xcrun --find libtool`

export SDK_ROOT=`xcrun --sdk iphoneos --show-sdk-path`
#export SDK_ROOT=`xcrun --sdk iphonesimulator --show-sdk-path`

# ios toolchains cmake
if [ ! -d "ios-cmake" ]; then
    git clone https://github.com/leetal/ios-cmake.git
fi
CMAKE_TOOLCHAIN_FILE=${CURRENT_DIRECTORY}/ios-cmake/ios.toolchain.cmake

rm -fr ${BUILD_DIR}
mkdir -p ${LIB_ROOT_DIR}
mkdir -p ${INSTALL_DIR}
mkdir -p ${SOURCE_DIR}
mkdir -p ${BUILD_DIR}

# ======================================install libs=====================================
LIB_VERSION=5.4.4
LIB_NAME=lua-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

# 安装目录不存在则进行安装
rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget http://www.lua.org/ftp/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    cd ${LIB_SRC_DIR}

	sed -i '9s? gcc -std=gnu99?'" ${CC}"'?' ${LIB_SRC_DIR}/src/Makefile
	sed -i '10s/ / -fPIC /' ${LIB_SRC_DIR}/src/Makefile
	sed -i '14s? ar?'" ${AR}"'?' ${LIB_SRC_DIR}/src/Makefile
	sed -i '15s? ranlib?'" ${RANLIB}"'?' ${LIB_SRC_DIR}/src/Makefile

	make clean
    make 
    make local 
	mkdir -p ${LIB_INSTALL_DIR}

    mv ${LIB_SRC_DIR}/install/include ${LIB_INSTALL_DIR}
    mv ${LIB_SRC_DIR}/install/lib ${LIB_INSTALL_DIR}

    cd -
fi

# =================================Install SDL2===================================
LIB_VERSION=2.0.22
LIB_NAME=SDL2-${LIB_VERSION}
LIB_SRC_TAR_GZ=${SOURCE_DIR}/${LIB_NAME}.tar.gz 
LIB_SRC_DIR=${SOURCE_DIR}/${LIB_NAME}
LIB_BUILD_DIR=${BUILD_DIR}/${LIB_NAME}
LIB_INSTALL_DIR=${INSTALL_DIR}/${LIB_NAME}

#rm -fr ${LIB_INSTALL_DIR}
if [ ! -d ${LIB_INSTALL_DIR} ]; then
    if [ ! -d ${LIB_SRC_DIR} ]; then
        if [ -f ${LIB_SRC_TAR_GZ} ]; then
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        else
            wget https://ghproxy.com/https://github.com/libsdl-org/SDL/releases/download/release-${LIB_VERSION}/${LIB_NAME}.tar.gz -O ${LIB_SRC_TAR_GZ}
            tar -vxf ${LIB_SRC_TAR_GZ} -C ${SOURCE_DIR}
        fi
    fi

    mkdir -p ${LIB_BUILD_DIR}
    cmake -B ${LIB_BUILD_DIR} -S ${LIB_SRC_DIR} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DPLATFORM=SIMULATOR64 -DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR}
    cmake --build ${LIB_BUILD_DIR}
    cmake --install ${LIB_BUILD_DIR}
	#cd ${LIB_SRC_DIR}
	#./configure  --enable-static --enable-shared=no --enable-hidapi=no --with-pic --prefix=${LIB_INSTALL_DIR} --host=${TARGET} --with-sysroot=${SYSROOT}
	#make 
	#make install
    #cd -
fi
